package com.asia5b.a5mall.middleware.logic.viewmodel.member

import android.app.Activity
import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.model.bean.ConnectLoginBean
import com.asia5b.a5mall.middleware.model.member.MemberModel
import com.asia5b.a5mall.middleware.utils.MD5Util
import com.asia5b.a5mall.middleware.utils.toJsonObject
import com.asia5b.a5mall.middleware.utils.valueBool
import io.reactivex.Observable
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/4/9.
 */
class ThridLoginFragmentVM : SimpleViewModel() {

    /**
     * 第三方登录而已
     */
    fun authLogin(type: String, openId: String) {
        val observable = passportApi.authLogin(type, openId)
                .flatMap { responseBody ->
                    val isBind = responseBody.toJsonObject().valueBool("is_bind")
                    if (isBind) {
                        /*
                        {
                            "access_token": "eyJhbGciOiJIUzUxMiJ9.e
                            "refresh_token": "eyJhbGciOiJIUzUxMiJ9.
                            "uid": 214,
                            "is_bind": true
                        }
                         */
                        return@flatMap memberApi.memberInfo()
                    } else {
                        /*
                        {
                            "is_bind": false
                        }
                         */
                        //todo 返回空数据
                        return@flatMap Observable.just(MemberModel())
                    }
                }
        val netParams = SimpleNetParams()
        baseSubscribe(observable, netParams, Consumer { result ->
            if (result.member_id > 0) {
                UserRepository.setUserInfo(result as MemberModel, true)
//                providerView().loginSuccessResult()
                loginSuccessResultLiveData.value=true
            } else {
//                providerView().authLoginError()
                authLoginErrorLiveData.value=true
            }
        }, Consumer {
            authLoginErrorLiveData.value=true
//            providerView().authLoginError()
        })
    }

    val authLoginErrorLiveData = MutableLiveData<Boolean>()

    fun getAlipayAuthInfo(mActivity: Activity) {
//        val observable = AlipayHelper.getAlipayOpenId(
//                passportApi
//                .getAlipayAuthInfo()
//                .map {
//                    it.getJsonString()
//                }, mActivity)
//
//
//        request(observable, Consumer { result ->
//            providerView().alipayAuthInfo(result)
//        })
    }

    val loginSuccessResultLiveData = MutableLiveData<Boolean>()

    /**
     * 登录并绑定第三方uid
     */
    fun authBind(connectInfo: ConnectLoginBean, username: String, password: String, imageCode: String) {
        val observable = passportApi.authNormalBind(
                connectInfo!!.unionId,
                connectInfo!!.type,
                username,
                MD5Util.MD5Encode(password, null)!!,
                imageCode)
                .flatMap { return@flatMap memberApi.memberInfo() }
        baseSubscribe(observable, Consumer { result ->
            UserRepository.setUserInfo(result, true)
//            providerView().loginSuccessResult()
            loginSuccessResultLiveData.value=true
        })
    }
}